//
//  LogViewController.swift
//  YDTestTool
//
//  Created by 赵宏亚 on 2019/11/10.
//  Copyright © 2019 zptec. All rights reserved.
//

import UIKit
import WebKit

class LogViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    var webview: WKWebView!
    var contentController: WKUserContentController = WKUserContentController()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "云钉绘途"
        
        webview = WKWebView()
        webview.uiDelegate = self
        webview.navigationDelegate = self
        webview.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height+100)
        
        let myURL = URL(string: "http://confluence.zpt/pages/viewpage.action?pageId=12616041")
        let myRequest = URLRequest(url: myURL!)
        webview.load(myRequest)
        self.view.addSubview(webview)
        
        
        if #available(iOS 13.0, *) {
        } else {
            let backBtn = UIButton.init(type: .custom)
            backBtn.frame = CGRect.init(x:UIScreen.main.bounds.size.width-80, y: UIApplication.shared.statusBarFrame.size.height+6, width: 70, height: 28)
            backBtn.backgroundColor = UIColor.red
            backBtn.layer.masksToBounds = true
            backBtn.layer.cornerRadius = 14
            backBtn.setTitle("返回", for: .normal)
            backBtn.titleLabel?.font = .systemFont(ofSize: 15)
            backBtn.addTarget(self, action: #selector(backClick), for: .touchUpInside)
            webview.addSubview(backBtn)
        }
        
    }
    
    @objc func backClick() {
        self.dismiss(animated: true, completion: nil)
    }
    
    /**
         * WKNavigationDelegate
         */
        /* 页面开始加载时调用 */
        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
            print("页面开始加载")
        }
        
        /* 当内容开始返回时调用 */
        func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
            print("内容开始返回")
        }
        
        /* 页面加载完成后时调用 */
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        }
        
        /* 页面加载失败时调用 */
        func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
            print("网页加载失败")
        }
        
        //MARK: 跳转支付宝支付方法
        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            
            let requrl = navigationAction.request.url
            let scheme = requrl?.scheme
            
            let urlString = String(describing: requrl)
            
            print("requrl ********** \(urlString)")
            
            // 截取itms-apps://itunes.apple.com weixin:
            if (urlString.contains("itunes.apple.com")
                || urlString.contains("itms-services:")
                || urlString.contains("mailto:")
                || urlString.contains("sms:")
                || urlString.contains("tel:")
                || urlString.contains("taobao:")
                || urlString.contains("weixin:")
                || urlString.contains("pinduoduo:")
                || urlString.contains("alipay:")
                || urlString.contains("alipays:")
                || urlString.contains("mqq:")
                || urlString.contains("mqqapi:")) {
                
                let jumpSucc = UIApplication.shared.openURL(requrl!)
                if jumpSucc {
                    print("跳转成功了")
                    decisionHandler(.cancel)
                }
                return
            }
            decisionHandler(.allow)
        }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
